<script setup lang="ts">
import { reactive, computed } from 'vue'
import {
  ElButton,
  ElForm,
  ElRow,
  ElInput,
  ElInputNumber,
  ElSwitch,
  ElSelect,
  ElOption
} from 'element-plus'
// import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { createStatesApi, updateStatesApi, getStatesDetailApi } from '@/api/state'
import { getCountriesListApi } from '@/api/country'
import { State } from '@/api/state/types'
import { Country } from '@/api/country/types'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { FormItem } from '@/components/Form'
import { useValidator } from '@/hooks/web/useValidator'
import { useCreate } from '@/hooks/web/useCreate'
import { useRoute } from 'vue-router'
import { onMounted } from 'vue'

const { push } = useRouter()

const back = () => {
  push('/geography/state')
}

const { required } = useValidator()

const instance: State = reactive(new State())
const route = useRoute()

const regions = reactive<Array<Country>>([])

const { elFormRef, openMode, save, loading } = useCreate({
  instance,
  route,
  afterSave: back,
  createApi: createStatesApi,
  updateApi: updateStatesApi
})

const rules = reactive({
  code: [required('请填写代码')],
  name: [required('请填写名称')],
  country_id: [required('请选择国家')]
})

const title = computed(() => {
  return openMode.value === 'CREATE' ? '添加省份' : '编辑省份'
})

onMounted(async () => {
  if (openMode.value === 'UPDATE') {
    const { data } = await getStatesDetailApi(route.params.id as string)
    if (regions.length === 0) {
      regions.push(data.country as Country)
    }
    instance.init(data)
  }

  getCountriesListApi({ pageIndex: -1, order: 'sequence', disabled: 'N' }).then((res) => {
    Object.assign(regions, res.data.rows)
  })
})
</script>

<template>
  <ContentDetailWrap :title="title" @back="back">
    <template #right>
      <ElButton type="primary" :loading="loading" @click="save"> 保存 </ElButton>
    </template>

    <ElForm ref="elFormRef" :model="instance" label-width="100px" :rules="rules">
      <ElRow style="max-width: 450px; margin: 0 auto">
        <FormItem :col-props="{ span: 24 }" label="代码" prop="code">
          <ElInput v-model="instance.code" maxlength="50" :disabled="openMode === 'UPDATE'" />
        </FormItem>

        <FormItem :col-props="{ span: 24 }" label="名称" prop="name">
          <ElInput v-model="instance.name" maxlength="200" />
        </FormItem>

        <FormItem :col-props="{ span: 24 }" label="国家及地区" prop="country_id">
          <ElSelect v-model="instance.country_id">
            <ElOption v-for="item of regions" :key="item.id" :value="item.id!" :label="item.name" />
          </ElSelect>
        </FormItem>

        <FormItem :col-props="{ span: 24 }" label-width="100px" prop="disabled">
          <ElSwitch v-model="instance.disabled" active-text="禁用" />
        </FormItem>

        <FormItem :col-props="{ span: 24 }" label="序号" prop="sequence">
          <ElInputNumber v-model="instance.sequence" controls-position="right" :min="1" />
        </FormItem>
      </ElRow>
    </ElForm>
  </ContentDetailWrap>
</template>
